<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">
  <meta http-equiv="Cache-Control" content="no-transform">
  <meta http-equiv="Cache-Control" content="no-siteapp">
  <meta name="msvalidate.01" content="3B3E06144BB6363961FBE583C9BA49E6">
  <meta name="baidu-site-verification" content="2IGNE9f4OxLiq11O">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">
  <link rel="stylesheet" href="//cdn.jsdelivr.net/gh/fancyapps/fancybox@3/dist/jquery.fancybox.min.css">
  <link rel="stylesheet" href="/lib/pace/pace-theme-loading-bar.min.css">
  <script src="/lib/pace/pace.min.js"></script>

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"atan2021.gitee.io","root":"/","scheme":"Mist","version":"7.8.0","exturl":true,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":true,"show_result":true,"style":"default"},"back2top":{"enable":true,"sidebar":false,"scrollpercent":true},"bookmark":{"enable":true,"color":"#222","save":"auto"},"fancybox":true,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="本篇前提说明因为我这边做的是表情识别，另外一边做的是贪吃蛇的动作。贪吃蛇的食物就是我的表情，所以要把两者的数据连接起来。 贪吃蛇用JAVA来做，我用C++，然后两者可以用Socket来进行数据连接。设计时候是贪吃蛇用客户端，我这边是服务端。 本项目在该文档的基础上修改，https:&#x2F;&#x2F;blog.csdn.net&#x2F;qq_27923041&#x2F;article&#x2F;details&#x2F;83857964，谢谢这位作者">
<meta property="og:type" content="article">
<meta property="og:title" content="ARS展览项目（六）——Socket通信">
<meta property="og:url" content="https://atan2021.gitee.io/2021/ARS%E5%B1%95%E8%A7%88%E9%A1%B9%E7%9B%AE%EF%BC%88%E5%85%AD%EF%BC%89%E2%80%94%E2%80%94Socket%E9%80%9A%E4%BF%A1/index.html">
<meta property="og:site_name" content="博客">
<meta property="og:description" content="本篇前提说明因为我这边做的是表情识别，另外一边做的是贪吃蛇的动作。贪吃蛇的食物就是我的表情，所以要把两者的数据连接起来。 贪吃蛇用JAVA来做，我用C++，然后两者可以用Socket来进行数据连接。设计时候是贪吃蛇用客户端，我这边是服务端。 本项目在该文档的基础上修改，https:&#x2F;&#x2F;blog.csdn.net&#x2F;qq_27923041&#x2F;article&#x2F;details&#x2F;83857964，谢谢这位作者">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2021-09-05T09:23:01.000Z">
<meta property="article:modified_time" content="2021-09-05T09:52:00.143Z">
<meta property="article:author" content="陈政任">
<meta property="article:tag" content="ARS项目">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="https://atan2021.gitee.io/2021/ARS%E5%B1%95%E8%A7%88%E9%A1%B9%E7%9B%AE%EF%BC%88%E5%85%AD%EF%BC%89%E2%80%94%E2%80%94Socket%E9%80%9A%E4%BF%A1/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>ARS展览项目（六）——Socket通信 | 博客</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">博客</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>所有博客</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>
  <a role="button" class="book-mark-link book-mark-link-fixed"></a>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://atan2021.gitee.io/2021/ARS%E5%B1%95%E8%A7%88%E9%A1%B9%E7%9B%AE%EF%BC%88%E5%85%AD%EF%BC%89%E2%80%94%E2%80%94Socket%E9%80%9A%E4%BF%A1/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="陈政任">
      <meta itemprop="description" content="what atan">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="博客">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          ARS展览项目（六）——Socket通信
        </h1>

        <div class="post-meta">

	  
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>
              

              <time title="创建时间：2021-09-05 17:23:01 / 修改时间：17:52:00" itemprop="dateCreated datePublished" datetime="2021-09-05T17:23:01+08:00">2021-09-05</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/ARS%E9%A1%B9%E7%9B%AE/" itemprop="url" rel="index"><span itemprop="name">ARS项目</span></a>
                </span>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h1 id="本篇前提说明"><a href="#本篇前提说明" class="headerlink" title="本篇前提说明"></a>本篇前提说明</h1><p>因为我这边做的是表情识别，另外一边做的是贪吃蛇的动作。贪吃蛇的食物就是我的表情，所以要把两者的数据连接起来。</p>
<p>贪吃蛇用JAVA来做，我用C++，然后两者可以用Socket来进行数据连接。设计时候是贪吃蛇用客户端，<strong>我这边是服务端</strong>。</p>
<p>本项目在该文档的基础上修改，<span class="exturl" data-url="aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzI3OTIzMDQxL2FydGljbGUvZGV0YWlscy84Mzg1Nzk2NA==">https://blog.csdn.net/qq_27923041/article/details/83857964<i class="fa fa-external-link-alt"></i></span>，谢谢这位作者的帮助。<br><span id="more"></span></p>
<h1 id="先放上代码"><a href="#先放上代码" class="headerlink" title="先放上代码"></a>先放上代码</h1><h2 id="服务端的代码-Server端"><a href="#服务端的代码-Server端" class="headerlink" title="服务端的代码(Server端)"></a>服务端的代码(Server端)</h2><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//#include &quot;pch.h&quot;</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;winsock.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">pragma</span> comment(lib,<span class="meta-string">&quot;ws2_32.lib&quot;</span>)</span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">initialization</span><span class="params">()</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>&#123;</span><br><span class="line">	<span class="comment">//定义长度变量</span></span><br><span class="line">	<span class="keyword">int</span> send_len = <span class="number">0</span>;</span><br><span class="line">	<span class="keyword">int</span> recv_len = <span class="number">0</span>;</span><br><span class="line">	<span class="keyword">int</span> len = <span class="number">0</span>;</span><br><span class="line">	<span class="comment">//定义发送缓冲区和接受缓冲区</span></span><br><span class="line">	<span class="keyword">char</span> send_buf[<span class="number">100</span>];</span><br><span class="line">	<span class="keyword">char</span> recv_buf[<span class="number">100</span>];</span><br><span class="line">	<span class="comment">//定义服务端套接字，接受请求套接字</span></span><br><span class="line">	SOCKET s_server;</span><br><span class="line">	SOCKET s_accept;</span><br><span class="line">	<span class="comment">//服务端地址客户端地址</span></span><br><span class="line">	SOCKADDR_IN server_addr;</span><br><span class="line">	SOCKADDR_IN accept_addr;</span><br><span class="line">	<span class="built_in">initialization</span>();</span><br><span class="line">	<span class="comment">//填充服务端信息</span></span><br><span class="line">	server_addr.sin_family = AF_INET;</span><br><span class="line">	server_addr.sin_addr.S_un.S_addr = <span class="built_in">htonl</span>(INADDR_ANY);</span><br><span class="line">	server_addr.sin_port = <span class="built_in">htons</span>(<span class="number">5010</span>);</span><br><span class="line">	<span class="comment">//创建套接字</span></span><br><span class="line">	s_server = <span class="built_in">socket</span>(AF_INET, SOCK_STREAM, <span class="number">0</span>);</span><br><span class="line">	<span class="keyword">if</span> (<span class="built_in">bind</span>(s_server, (SOCKADDR *)&amp;server_addr, <span class="built_in"><span class="keyword">sizeof</span></span>(SOCKADDR)) == SOCKET_ERROR) &#123;</span><br><span class="line">		cout &lt;&lt; <span class="string">&quot;套接字绑定失败！&quot;</span> &lt;&lt; endl;</span><br><span class="line">		<span class="built_in">WSACleanup</span>();</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">else</span> &#123;</span><br><span class="line">		cout &lt;&lt; <span class="string">&quot;套接字绑定成功！&quot;</span> &lt;&lt; endl;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">//设置套接字为监听状态</span></span><br><span class="line">	<span class="keyword">if</span> (<span class="built_in">listen</span>(s_server, SOMAXCONN) &lt; <span class="number">0</span>) &#123;</span><br><span class="line">		cout &lt;&lt; <span class="string">&quot;设置监听状态失败！&quot;</span> &lt;&lt; endl;</span><br><span class="line">		<span class="built_in">WSACleanup</span>();</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">else</span> &#123;</span><br><span class="line">		cout &lt;&lt; <span class="string">&quot;设置监听状态成功！&quot;</span> &lt;&lt; endl;</span><br><span class="line">	&#125;</span><br><span class="line">	cout &lt;&lt; <span class="string">&quot;服务端正在监听连接，请稍候....&quot;</span> &lt;&lt; endl;</span><br><span class="line">	<span class="comment">//接受连接请求</span></span><br><span class="line">	len = <span class="built_in"><span class="keyword">sizeof</span></span>(SOCKADDR);</span><br><span class="line">	s_accept = <span class="built_in">accept</span>(s_server, (SOCKADDR *)&amp;accept_addr, &amp;len);</span><br><span class="line">	<span class="keyword">if</span> (s_accept == SOCKET_ERROR) &#123;</span><br><span class="line">		cout &lt;&lt; <span class="string">&quot;连接失败！&quot;</span> &lt;&lt; endl;</span><br><span class="line">		<span class="built_in">WSACleanup</span>();</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	cout &lt;&lt; <span class="string">&quot;连接建立，准备接受数据&quot;</span> &lt;&lt; endl;</span><br><span class="line">	<span class="comment">//接收数据</span></span><br><span class="line">	<span class="keyword">while</span> (<span class="number">1</span>) &#123;</span><br><span class="line">		recv_len = <span class="built_in">recv</span>(s_accept, recv_buf, <span class="number">100</span>, <span class="number">0</span>);</span><br><span class="line">		<span class="keyword">if</span> (recv_len &lt; <span class="number">0</span>) &#123;</span><br><span class="line">			cout &lt;&lt; <span class="string">&quot;接受失败！&quot;</span> &lt;&lt; endl;</span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">else</span> &#123;</span><br><span class="line">			cout &lt;&lt; <span class="string">&quot;客户端信息:&quot;</span> &lt;&lt; recv_buf &lt;&lt; endl;</span><br><span class="line">		&#125;</span><br><span class="line">		cout &lt;&lt; <span class="string">&quot;请输入回复信息:&quot;</span>;</span><br><span class="line">		cin &gt;&gt; send_buf;</span><br><span class="line">		send_len = <span class="built_in">send</span>(s_accept, send_buf, <span class="number">100</span>, <span class="number">0</span>);</span><br><span class="line">		<span class="keyword">if</span> (send_len &lt; <span class="number">0</span>) &#123;</span><br><span class="line">			cout &lt;&lt; <span class="string">&quot;发送失败！&quot;</span> &lt;&lt; endl;</span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">//关闭套接字</span></span><br><span class="line">	<span class="built_in">closesocket</span>(s_server);</span><br><span class="line">	<span class="built_in">closesocket</span>(s_accept);</span><br><span class="line">	<span class="comment">//释放DLL资源</span></span><br><span class="line">	<span class="built_in">WSACleanup</span>();</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">initialization</span><span class="params">()</span> </span>&#123;</span><br><span class="line">	<span class="comment">//初始化套接字库</span></span><br><span class="line">	WORD w_req = <span class="built_in">MAKEWORD</span>(<span class="number">2</span>, <span class="number">2</span>);<span class="comment">//版本号</span></span><br><span class="line">	WSADATA wsadata;</span><br><span class="line">	<span class="keyword">int</span> err;</span><br><span class="line">	err = <span class="built_in">WSAStartup</span>(w_req, &amp;wsadata);</span><br><span class="line">	<span class="keyword">if</span> (err != <span class="number">0</span>) &#123;</span><br><span class="line">		cout &lt;&lt; <span class="string">&quot;初始化套接字库失败！&quot;</span> &lt;&lt; endl;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">else</span> &#123;</span><br><span class="line">		cout &lt;&lt; <span class="string">&quot;初始化套接字库成功！&quot;</span> &lt;&lt; endl;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">//检测版本号</span></span><br><span class="line">	<span class="keyword">if</span> (<span class="built_in">LOBYTE</span>(wsadata.wVersion) != <span class="number">2</span> || <span class="built_in">HIBYTE</span>(wsadata.wHighVersion) != <span class="number">2</span>) &#123;</span><br><span class="line">		cout &lt;&lt; <span class="string">&quot;套接字库版本号不符！&quot;</span> &lt;&lt; endl;</span><br><span class="line">		<span class="built_in">WSACleanup</span>();</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">else</span> &#123;</span><br><span class="line">		cout &lt;&lt; <span class="string">&quot;套接字库版本正确！&quot;</span> &lt;&lt; endl;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">//填充服务端地址信息</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="客户端代码-CLient端）"><a href="#客户端代码-CLient端）" class="headerlink" title="客户端代码(CLient端）"></a>客户端代码(CLient端）</h2><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//#include &quot;pch.h&quot;</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;winsock.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">pragma</span> comment(lib,<span class="meta-string">&quot;ws2_32.lib&quot;</span>)</span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">initialization</span><span class="params">()</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>&#123;</span><br><span class="line">	<span class="comment">//定义长度变量</span></span><br><span class="line">	<span class="keyword">int</span> send_len = <span class="number">0</span>;</span><br><span class="line">	<span class="keyword">int</span> recv_len = <span class="number">0</span>;</span><br><span class="line">	<span class="comment">//定义发送缓冲区和接受缓冲区</span></span><br><span class="line">	<span class="keyword">char</span> send_buf[<span class="number">100</span>];</span><br><span class="line">	<span class="keyword">char</span> recv_buf[<span class="number">100</span>];</span><br><span class="line">	<span class="comment">//定义服务端套接字，接受请求套接字</span></span><br><span class="line">	SOCKET s_server;</span><br><span class="line">	<span class="comment">//服务端地址客户端地址</span></span><br><span class="line">	SOCKADDR_IN server_addr;</span><br><span class="line">	<span class="built_in">initialization</span>();</span><br><span class="line">	<span class="comment">//填充服务端信息</span></span><br><span class="line">	server_addr.sin_family = AF_INET;</span><br><span class="line">	server_addr.sin_addr.S_un.S_addr = <span class="built_in">inet_addr</span>(<span class="string">&quot;127.0.0.1&quot;</span>);</span><br><span class="line">	server_addr.sin_port = <span class="built_in">htons</span>(<span class="number">5010</span>);</span><br><span class="line">	<span class="comment">//创建套接字</span></span><br><span class="line">	s_server = <span class="built_in">socket</span>(AF_INET, SOCK_STREAM, <span class="number">0</span>);</span><br><span class="line">	<span class="keyword">if</span> (<span class="built_in">connect</span>(s_server, (SOCKADDR *)&amp;server_addr, <span class="built_in"><span class="keyword">sizeof</span></span>(SOCKADDR)) == SOCKET_ERROR) &#123;</span><br><span class="line">		cout &lt;&lt; <span class="string">&quot;服务器连接失败！&quot;</span> &lt;&lt; endl;</span><br><span class="line">		<span class="built_in">WSACleanup</span>();</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">else</span> &#123;</span><br><span class="line">		cout &lt;&lt; <span class="string">&quot;服务器连接成功！&quot;</span> &lt;&lt; endl;</span><br><span class="line">	&#125;</span><br><span class="line"> </span><br><span class="line">	<span class="comment">//发送,接收数据</span></span><br><span class="line">	<span class="keyword">while</span> (<span class="number">1</span>) &#123;</span><br><span class="line">		cout &lt;&lt; <span class="string">&quot;请输入发送信息:&quot;</span>;</span><br><span class="line">		cin &gt;&gt; send_buf;</span><br><span class="line">		send_len = <span class="built_in">send</span>(s_server, send_buf, <span class="number">100</span>, <span class="number">0</span>);</span><br><span class="line">		<span class="keyword">if</span> (send_len &lt; <span class="number">0</span>) &#123;</span><br><span class="line">			cout &lt;&lt; <span class="string">&quot;发送失败！&quot;</span> &lt;&lt; endl;</span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		&#125;</span><br><span class="line">		recv_len = <span class="built_in">recv</span>(s_server, recv_buf, <span class="number">100</span>, <span class="number">0</span>);</span><br><span class="line">		<span class="keyword">if</span> (recv_len &lt; <span class="number">0</span>) &#123;</span><br><span class="line">			cout &lt;&lt; <span class="string">&quot;接受失败！&quot;</span> &lt;&lt; endl;</span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">else</span> &#123;</span><br><span class="line">			cout &lt;&lt; <span class="string">&quot;服务端信息:&quot;</span> &lt;&lt; recv_buf &lt;&lt; endl;</span><br><span class="line">		&#125; </span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">//关闭套接字</span></span><br><span class="line">	<span class="built_in">closesocket</span>(s_server);</span><br><span class="line">	<span class="comment">//释放DLL资源</span></span><br><span class="line">	<span class="built_in">WSACleanup</span>();</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">initialization</span><span class="params">()</span> </span>&#123;</span><br><span class="line">	<span class="comment">//初始化套接字库</span></span><br><span class="line">	WORD w_req = <span class="built_in">MAKEWORD</span>(<span class="number">2</span>, <span class="number">2</span>);<span class="comment">//版本号</span></span><br><span class="line">	WSADATA wsadata;</span><br><span class="line">	<span class="keyword">int</span> err;</span><br><span class="line">	err = <span class="built_in">WSAStartup</span>(w_req, &amp;wsadata);</span><br><span class="line">	<span class="keyword">if</span> (err != <span class="number">0</span>) &#123;</span><br><span class="line">		cout &lt;&lt; <span class="string">&quot;初始化套接字库失败！&quot;</span> &lt;&lt; endl;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">else</span> &#123;</span><br><span class="line">		cout &lt;&lt; <span class="string">&quot;初始化套接字库成功！&quot;</span> &lt;&lt; endl;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">//检测版本号</span></span><br><span class="line">	<span class="keyword">if</span> (<span class="built_in">LOBYTE</span>(wsadata.wVersion) != <span class="number">2</span> || <span class="built_in">HIBYTE</span>(wsadata.wHighVersion) != <span class="number">2</span>) &#123;</span><br><span class="line">		cout &lt;&lt; <span class="string">&quot;套接字库版本号不符！&quot;</span> &lt;&lt; endl;</span><br><span class="line">		<span class="built_in">WSACleanup</span>();</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">else</span> &#123;</span><br><span class="line">		cout &lt;&lt; <span class="string">&quot;套接字库版本正确！&quot;</span> &lt;&lt; endl;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">//填充服务端地址信息</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h1 id="说明"><a href="#说明" class="headerlink" title="说明"></a>说明</h1><p>因为我这边做的是表情识别，另外一边做的是贪吃蛇的动作。贪吃蛇的食物就是我的表情，所以要把两者的数据连接起来。</p>
<p>贪吃蛇用JAVA来做，我用C++，然后两者可以用Socket来进行数据连接。设计时候是贪吃蛇用客户端，<strong>我这边是服务端</strong>。</p>
<p>设计了:</p>
<style>
table th:nth-of-type(1) {width: 200px;}
th:nth-of-type(2) {width: 200px;}
table {margin-left: auto; margin-right: auto}
</style>

<div class="table-container">
<table>
<thead>
<tr>
<th style="text-align:center">发送数据</th>
<th style="text-align:center">代表表情</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center">1</td>
<td style="text-align:center">开心(happy)</td>
</tr>
<tr>
<td style="text-align:center">2</td>
<td style="text-align:center">平常(common)</td>
</tr>
<tr>
<td style="text-align:center">3</td>
<td style="text-align:center">厌恶(disgust)</td>
</tr>
<tr>
<td style="text-align:center">4</td>
<td style="text-align:center">生气(angry)</td>
</tr>
</tbody>
</table>
</div>
<p>这里之所以放客户端的代码，我是拿来测试服务端改得好不好的，所以整个项目过程中我都没有改过客户端的代码，生成一个exe就可以一直用。</p>
<p>程序直接新建一个C++空项目，导入opencv和dlib属性表，加入cpp源文件就可以用。建立两个项目，一个服务端，一个客户端。</p>
<h1 id="解决的一个难点"><a href="#解决的一个难点" class="headerlink" title="解决的一个难点"></a>解决的一个难点</h1><p>Socket通信的问题容易解决，让它改成自动发送数据也好解决，但是整合的时候发现！！Socket加入opencv或dlib的库编译时，会无法建立监听。</p>
<p>后来一步步排除原因，有计算机的同学帮忙，发现是socket和opencv与dlib的命名空间发生了冲突，就是<code>using namespace stu;</code>不正常。</p>
<p>于是计算机的同学想到一个很好的方法，把Socket写在不同文件中，再写一个头文件来引用，那么两者的命名空间便不会冲突，因为是不同的cpp文件，如下：</p>
<h2 id="新建一个头文件-1-h"><a href="#新建一个头文件-1-h" class="headerlink" title="新建一个头文件 1.h"></a>新建一个头文件 1.h</h2><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">pragma</span> once</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">ifndef</span> XXX_H</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> XXX_H</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">aaa</span><span class="params">(<span class="keyword">int</span> ax)</span></span>;</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">endif</span></span></span><br></pre></td></tr></table></figure>
<h2 id="新建一个1-cpp-拿来放Socket的代码"><a href="#新建一个1-cpp-拿来放Socket的代码" class="headerlink" title="新建一个1.cpp 拿来放Socket的代码"></a>新建一个1.cpp 拿来放Socket的代码</h2><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&quot;1.h&quot;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;winsock.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">pragma</span> comment(lib,<span class="meta-string">&quot;ws2_32.lib&quot;</span>)</span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">initialization</span><span class="params">()</span></span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">extern</span> <span class="keyword">int</span> b_int;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">aaa</span><span class="params">(<span class="keyword">int</span> a)</span> </span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	a = b_int;</span><br><span class="line">	<span class="comment">//定义长度变量</span></span><br><span class="line">	<span class="keyword">int</span> send_len = <span class="number">0</span>;</span><br><span class="line">	<span class="keyword">int</span> recv_len = <span class="number">0</span>;</span><br><span class="line">	<span class="keyword">int</span> len = <span class="number">0</span>;</span><br><span class="line">	<span class="comment">//定义发送缓冲区和接受缓冲区</span></span><br><span class="line">	<span class="keyword">char</span> send_buf[<span class="number">100</span>];</span><br><span class="line">	<span class="keyword">char</span> recv_buf[<span class="number">100</span>];</span><br><span class="line">	<span class="comment">//定义服务端套接字，接受请求套接字</span></span><br><span class="line">	SOCKET s_server;</span><br><span class="line">	SOCKET s_accept;</span><br><span class="line">	<span class="comment">//服务端地址客户端地址</span></span><br><span class="line">	SOCKADDR_IN server_addr;</span><br><span class="line">	SOCKADDR_IN accept_addr;</span><br><span class="line">	<span class="built_in">initialization</span>();</span><br><span class="line">	<span class="comment">//填充服务端信息</span></span><br><span class="line">	server_addr.sin_family = AF_INET;</span><br><span class="line">	server_addr.sin_addr.S_un.S_addr = <span class="built_in">htonl</span>(INADDR_ANY);</span><br><span class="line">	server_addr.sin_port = <span class="built_in">htons</span>(<span class="number">5010</span>);</span><br><span class="line"></span><br><span class="line">	<span class="comment">//创建套接字</span></span><br><span class="line">	s_server = <span class="built_in">socket</span>(AF_INET, SOCK_STREAM, <span class="number">0</span>);</span><br><span class="line">	<span class="built_in">bind</span>(s_server, (SOCKADDR *)&amp;server_addr, <span class="built_in"><span class="keyword">sizeof</span></span>(SOCKADDR));</span><br><span class="line"></span><br><span class="line">	<span class="comment">//设置套接字为监听状态</span></span><br><span class="line">	<span class="built_in">listen</span>(s_server, SOMAXCONN);</span><br><span class="line"></span><br><span class="line">	<span class="comment">//接受连接请求</span></span><br><span class="line">	len = <span class="built_in"><span class="keyword">sizeof</span></span>(SOCKADDR);</span><br><span class="line">	s_accept = <span class="built_in">accept</span>(s_server, (SOCKADDR *)&amp;accept_addr, &amp;len);</span><br><span class="line"></span><br><span class="line">	<span class="comment">//接收数据</span></span><br><span class="line">	<span class="keyword">while</span> (<span class="number">1</span>)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">if</span> (a == <span class="number">1</span>) </span><br><span class="line">		&#123;</span><br><span class="line">			send_buf[<span class="number">0</span>] = <span class="string">&#x27;1&#x27;</span>;</span><br><span class="line">			send_buf[<span class="number">1</span>] = <span class="string">&#x27;\0&#x27;</span>;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">else</span> <span class="keyword">if</span> (a == <span class="number">2</span>)</span><br><span class="line">		&#123;</span><br><span class="line">			send_buf[<span class="number">0</span>] = <span class="string">&#x27;2&#x27;</span>;</span><br><span class="line">			send_buf[<span class="number">1</span>] = <span class="string">&#x27;\0&#x27;</span>;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">else</span> <span class="keyword">if</span> (a == <span class="number">3</span>)</span><br><span class="line">		&#123;</span><br><span class="line">			send_buf[<span class="number">0</span>] = <span class="string">&#x27;3&#x27;</span>;</span><br><span class="line">			send_buf[<span class="number">1</span>] = <span class="string">&#x27;\0&#x27;</span>;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">else</span> <span class="keyword">if</span> (a == <span class="number">4</span>)</span><br><span class="line">		&#123;</span><br><span class="line">			send_buf[<span class="number">0</span>] = <span class="string">&#x27;4&#x27;</span>;</span><br><span class="line">			send_buf[<span class="number">1</span>] = <span class="string">&#x27;\0&#x27;</span>;</span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line">		send_len = <span class="built_in">send</span>(s_accept, send_buf, <span class="number">100</span>, <span class="number">0</span>);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">initialization</span><span class="params">()</span> </span>&#123;</span><br><span class="line">	<span class="comment">//初始化套接字库</span></span><br><span class="line">	WORD w_req = <span class="built_in">MAKEWORD</span>(<span class="number">2</span>, <span class="number">2</span>);<span class="comment">//版本号</span></span><br><span class="line">	WSADATA wsadata;</span><br><span class="line">	<span class="keyword">int</span> err;</span><br><span class="line">	err = <span class="built_in">WSAStartup</span>(w_req, &amp;wsadata);</span><br><span class="line">	<span class="keyword">if</span> (err != <span class="number">0</span>) &#123;</span><br><span class="line">		cout &lt;&lt; <span class="string">&quot;初始化套接字库失败！&quot;</span> &lt;&lt; endl;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">else</span> &#123;</span><br><span class="line">		cout &lt;&lt; <span class="string">&quot;初始化套接字库成功！&quot;</span> &lt;&lt; endl;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">//检测版本号</span></span><br><span class="line">	<span class="keyword">if</span> (<span class="built_in">LOBYTE</span>(wsadata.wVersion) != <span class="number">2</span> || <span class="built_in">HIBYTE</span>(wsadata.wHighVersion) != <span class="number">2</span>) &#123;</span><br><span class="line">		cout &lt;&lt; <span class="string">&quot;套接字库版本号不符！&quot;</span> &lt;&lt; endl;</span><br><span class="line">		<span class="built_in">WSACleanup</span>();</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">else</span> &#123;</span><br><span class="line">		cout &lt;&lt; <span class="string">&quot;套接字库版本正确！&quot;</span> &lt;&lt; endl;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">//填充服务端地址信息</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="新建一个2-cpp-拿来放main-函数"><a href="#新建一个2-cpp-拿来放main-函数" class="headerlink" title="新建一个2.cpp 拿来放main()函数"></a>新建一个2.cpp 拿来放main()函数</h2><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&quot;1.h&quot;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;thread&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;math.h&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="keyword">int</span> b_int = <span class="number">1</span>;</span><br><span class="line"><span class="function"><span class="keyword">extern</span> <span class="keyword">void</span> <span class="title">aaa</span><span class="params">(<span class="keyword">int</span> a)</span></span>;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="keyword">int</span> ccc = <span class="number">2</span> ;</span><br><span class="line">	b_int = ccc;</span><br><span class="line">	<span class="function">std::thread <span class="title">t</span><span class="params">(aaa, b_int)</span></span>;</span><br><span class="line">	t.<span class="built_in">join</span>();</span><br><span class="line">	<span class="comment">//aaa(b_int);</span></span><br><span class="line">	<span class="comment">//cout &lt;&lt;b_int &lt;&lt;endl ;</span></span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>其中，<code>std::thread t(aaa, b_int);t.join();</code>是多线程的动作，会在下一篇文章解释。<code>#include &lt;thread&gt;</code>是多线程的头文件。</p>
<p>在这里，多文件和写库就可以了。</p>

    </div>

    
    
    

      
      <div>
    
        <div style="text-align:center;color: #ccc;font-size:14px;">------ 本文结束------</div>
    
</div>
      
        

<div>
<ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>本文作者： </strong>陈政任
  </li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    <a href="https://atan2021.gitee.io/2021/ARS%E5%B1%95%E8%A7%88%E9%A1%B9%E7%9B%AE%EF%BC%88%E5%85%AD%EF%BC%89%E2%80%94%E2%80%94Socket%E9%80%9A%E4%BF%A1/" title="ARS展览项目（六）——Socket通信">https://atan2021.gitee.io/2021/ARS展览项目（六）——Socket通信/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <span class="exturl" data-url="aHR0cHM6Ly9jcmVhdGl2ZWNvbW1vbnMub3JnL2xpY2Vuc2VzL2J5LW5jLXNhLzQuMC8="><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</span> 许可协议。转载请注明出处！
  </li>
</ul>
</div>


      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/ARS%E9%A1%B9%E7%9B%AE/" rel="tag"><i class="fa fa-tag"></i> ARS项目</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2021/ARS%E5%B1%95%E8%A7%88%E9%A1%B9%E7%9B%AE%EF%BC%88%E4%BA%94%EF%BC%89%E2%80%94%E2%80%94%E8%A1%A8%E6%83%85%E8%AF%86%E5%88%AB%E6%B5%8B%E8%AF%95/" rel="prev" title="ARS展览项目（五）——表情识别测试">
      <i class="fa fa-chevron-left"></i> ARS展览项目（五）——表情识别测试
    </a></div>
      <div class="post-nav-item">
    <a href="/2021/ARS%E5%B1%95%E8%A7%88%E9%A1%B9%E7%9B%AE%EF%BC%88%E4%B8%83%EF%BC%89%E2%80%94%E2%80%94C-%E5%A4%9A%E7%BA%BF%E7%A8%8B%EF%BC%9ASocket-%E8%A1%A8%E6%83%85%E8%AF%86%E5%88%AB%E6%95%B4%E5%90%88/" rel="next" title="ARS展览项目（七）——C++多线程：Socket+表情识别整合">
      ARS展览项目（七）——C++多线程：Socket+表情识别整合 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E6%9C%AC%E7%AF%87%E5%89%8D%E6%8F%90%E8%AF%B4%E6%98%8E"><span class="nav-number">1.</span> <span class="nav-text">本篇前提说明</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%85%88%E6%94%BE%E4%B8%8A%E4%BB%A3%E7%A0%81"><span class="nav-number">2.</span> <span class="nav-text">先放上代码</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%9C%8D%E5%8A%A1%E7%AB%AF%E7%9A%84%E4%BB%A3%E7%A0%81-Server%E7%AB%AF"><span class="nav-number">2.1.</span> <span class="nav-text">服务端的代码(Server端)</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%AE%A2%E6%88%B7%E7%AB%AF%E4%BB%A3%E7%A0%81-CLient%E7%AB%AF%EF%BC%89"><span class="nav-number">2.2.</span> <span class="nav-text">客户端代码(CLient端）</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E8%AF%B4%E6%98%8E"><span class="nav-number">3.</span> <span class="nav-text">说明</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E8%A7%A3%E5%86%B3%E7%9A%84%E4%B8%80%E4%B8%AA%E9%9A%BE%E7%82%B9"><span class="nav-number">4.</span> <span class="nav-text">解决的一个难点</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%96%B0%E5%BB%BA%E4%B8%80%E4%B8%AA%E5%A4%B4%E6%96%87%E4%BB%B6-1-h"><span class="nav-number">4.1.</span> <span class="nav-text">新建一个头文件 1.h</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%96%B0%E5%BB%BA%E4%B8%80%E4%B8%AA1-cpp-%E6%8B%BF%E6%9D%A5%E6%94%BESocket%E7%9A%84%E4%BB%A3%E7%A0%81"><span class="nav-number">4.2.</span> <span class="nav-text">新建一个1.cpp 拿来放Socket的代码</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%96%B0%E5%BB%BA%E4%B8%80%E4%B8%AA2-cpp-%E6%8B%BF%E6%9D%A5%E6%94%BEmain-%E5%87%BD%E6%95%B0"><span class="nav-number">4.3.</span> <span class="nav-text">新建一个2.cpp 拿来放main()函数</span></a></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="陈政任"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">陈政任</p>
  <div class="site-description" itemprop="description">what atan</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">14</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">5</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">5</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">陈政任</span>
</div>
  <div class="powered-by">由 <span class="exturl theme-link" data-url="aHR0cHM6Ly9oZXhvLmlv">Hexo</span> & <span class="exturl theme-link" data-url="aHR0cHM6Ly9taXN0LnRoZW1lLW5leHQub3Jn">NexT.Mist</span> 强力驱动
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="//cdn.jsdelivr.net/npm/jquery@3/dist/jquery.min.js"></script>
  <script src="//cdn.jsdelivr.net/gh/fancyapps/fancybox@3/dist/jquery.fancybox.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/muse.js"></script>


<script src="/js/next-boot.js"></script>

<script src="/js/bookmark.js"></script>




  
  <script>
    (function(){
      var canonicalURL, curProtocol;
      //Get the <link> tag
      var x=document.getElementsByTagName("link");
		//Find the last canonical URL
		if(x.length > 0){
			for (i=0;i<x.length;i++){
				if(x[i].rel.toLowerCase() == 'canonical' && x[i].href){
					canonicalURL=x[i].href;
				}
			}
		}
    //Get protocol
	    if (!canonicalURL){
	    	curProtocol = window.location.protocol.split(':')[0];
	    }
	    else{
	    	curProtocol = canonicalURL.split(':')[0];
	    }
      //Get current URL if the canonical URL does not exist
	    if (!canonicalURL) canonicalURL = window.location.href;
	    //Assign script content. Replace current URL with the canonical URL
      !function(){var e=/([http|https]:\/\/[a-zA-Z0-9\_\.]+\.baidu\.com)/gi,r=canonicalURL,t=document.referrer;if(!e.test(r)){var n=(String(curProtocol).toLowerCase() === 'https')?"https://sp0.baidu.com/9_Q4simg2RQJ8t7jm9iCKT-xh_/s.gif":"//api.share.baidu.com/s.gif";t?(n+="?r="+encodeURIComponent(document.referrer),r&&(n+="&l="+r)):r&&(n+="?l="+r);var i=new Image;i.src=n}}(window);})();
  </script>




  
<script src="/js/local-search.js"></script>













  

  

  

<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","model":{"jsonPath":"/live2dw/assets/tororo.model.json"},"display":{"position":"right","width":180,"height":360},"mobile":{"show":false},"react":{"opacityDefault":1,"opacityOnHover":1},"log":false,"tagMode":false});</script></body>
</html>
